import re
import time

# Nginx日志格式
'''log_format  access_log_format  '"$remote_addr" "$remote_user" "[$time_local]" "$request" '
              '"$status" "$body_bytes_sent" "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for" "$http_host" "$request_time" "$upstream_response_time" "$upstream_addr" "$upstream_status"';   '''

log_line = '''"111.30.107.2" "-" "[28/Feb/2019:15:37:04 +0800]" "GET /center_h/latestActivities/listNew?pageNo=1&pageSize=4&time=1551339429208 HTTP/1.1" "200" "1644" "http://gxh.izhixue.org/index.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.6.0.18627" "-" "gxh.izhixue.org" "0.007" "0.007" "127.0.0.1:8080" "200"'''

# Nginx日志正则匹配
pattern_01 = r'''"(?P<remote_addr>[^"]+)" "(?P<remote_user>[^"]+)" "(?P<time_local>[^"]+)" "(?P<request>[^"]+)" "(?P<status>[^"]+)" "(?P<body_bytes_sent>[^"]+)" "(?P<http_referer>[^"]+)" "(?P<http_user_agent>[^"]+)" "(?P<http_x_forwarded_for>[^"]+)" "(?P<http_host>[^"]+)" "(?P<request_time>[^"]+)" "(?P<upstream_response_time>[^"]+)" "(?P<upstream_addr>[^"]+)" "(?P<upstream_status>[^"]+)"'''

log_file1 = "D:/k12日志分析测试/k12_center-01.log"


def main():
    # 读取日志文件
    start = time.time()
    regex = re.compile(pattern_01)
    with open(log_file1, 'r', encoding='utf-8') as f:
        for line in f:
            print(regex.search(line).group("remote_addr"))
    end = time.time()
    print('Running time: %.2f Seconds' % (end - start))



if __name__ == '__main__':
    main()
